package com.rongzhe.house.common.rest;

import com.rongzhe.house.common.constants.Constants;
import com.rongzhe.house.common.exception.RongZheBusinessException;
import com.rongzhe.house.common.resp.ApiResult;
import com.rongzhe.house.common.utils.QiniuFileUploadUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
@RequestMapping("file")
public class FileController {


    @ApiOperation(value = "上传", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<String> upload(HttpServletRequest req, @RequestParam(required = false) MultipartFile file) {

        ApiResult<String> resp = new ApiResult<>();
        try {
            String fileName = QiniuFileUploadUtil.uploadHeadImg(file);
            resp.setData(Constants.QINIU_HEAD_IMG_BUCKET_URL + "/" + fileName);
            resp.setMessage("上传成功");
        } catch (Exception e) {
            log.error("Fail to update user info", e);
            resp.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            resp.setMessage("内部错误");
        }
        return resp;
    }
}
